package com.zhoug.zmui.valuepair;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhoug.common.framework.glide.ZImageLoader;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.round.ZMUIRoundImageView;
import com.zhoug.zmui.utils.ZMUIUtils;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

/**
 * 选择框
 * zmui_style_ZMUIKeyValueImage
 *
 * @Author HK-LJJ
 * @Date 2020/3/23
 * @Description
 */
public class ZMUIKeyValueImage extends ZMUIAbsKeyValueView<View> {
    private static final String TAG = ">>>ZMUIKeyValueImage";
    private static final boolean DEBUG = false;
    private ZMUIRoundImageView imageView;
    private String path;
    private int placeHolder;
    private int imageWidth;
    private int imageHeight;

    public ZMUIKeyValueImage(Context context) {
        this(context, null);
    }

    public ZMUIKeyValueImage(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIKeyValueImage);
    }

    public ZMUIKeyValueImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void parseAttrs(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super.parseAttrs(context, attrs, defStyleAttr, defStyleRes);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZMUIKeyValueImage, defStyleAttr, R.style.zmui_style_ZMUIKeyValueImage);
        path = typedArray.getString(R.styleable.ZMUIKeyValueImage_zmui_path);
        placeHolder = typedArray.getResourceId(R.styleable.ZMUIKeyValueImage_zmui_placeHolder, ZMUIUtils.UN_USED_RES_ID);
        imageWidth = typedArray.getDimensionPixelOffset(R.styleable.ZMUIKeyValueImage_zmui_imageWidth, 60);
        imageHeight = typedArray.getDimensionPixelOffset(R.styleable.ZMUIKeyValueImage_zmui_imageHeight, 60);
        typedArray.recycle();
        if (DEBUG) {
            LogUtils.d(TAG, "parseAttrs:path=" + path);
            LogUtils.d(TAG, "parseAttrs:placeHolder=" + placeHolder);
            LogUtils.d(TAG, "parseAttrs:imageWidth=" + imageWidth);
            LogUtils.d(TAG, "parseAttrs:imageHeight=" + imageHeight);
        }
    }

    @Override
    public View getValueView() {
        if (valueView == null) {
            valueView = LayoutInflater.from(getContext()).inflate(R.layout.zmui_valuekey_image, this, false);
            imageView = valueView.findViewById(R.id.imageView);
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = imageWidth;
            layoutParams.height = imageHeight;
            imageView.setLayoutParams(layoutParams);
            imageView.setClickable(false);
            updateImage();
        }

        return valueView;
    }

    private void updateImage() {
        if (!StringUtils.isNullOrEmpty(path)) {
            ZImageLoader.load(getContext(), imageView, path,null,R.drawable.zmui_image_load_error,null);
        } else if (placeHolder != ZMUIUtils.UN_USED_RES_ID) {
            imageView.setImageResource(placeHolder);
        }
        onValueChange();
    }

    public ZMUIRoundImageView getImageView() {
        return imageView;
    }

    @Override
    public String getValue() {
        return path;
    }

    @Override
    public void setValue(CharSequence value) {
        this.path = (String) value;
        setError(null);
        updateImage();
    }

    public void setValue(CharSequence value, @DrawableRes int placeHolder) {
        this.path = (String) value;
        this.placeHolder =placeHolder;
        setError(null);
        updateImage();
    }

    @Override
    protected void _setStatusColor(int colorStatus) {

    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
    }


}
